<?php

$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('StretchHelper');
$StretchHelper = new StretchHelper();

$check = $ApiHelper->tokenCheck($this->Param);
if (!$check['success']) ErrorMsg::ApiMsg($check['msg'], $check['code']);

if (empty($this->Param['option'])) {
    
} else {
    switch ($this->Param['option']) {
        case 'list':
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('订单ID值错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
            
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
            $where = array(
                '`sr_oid` = ?' => $oid,
                '`sr_result` != ?' => 'del'
            );
            $data = $StretchHelper->signList($where, $page, $rp, $this->Param);
            $json_data = array('total' => $data['count'], 'list' => array(), 'page' => $page, 'pagetotal' => $data['pagetotal']);
            if (!empty($data['all'])){
                foreach ($data['all'] AS $rowNum => $row) {
                    $json_data['list'][] = $row;
                }
            }
            $ApiHelper->logSave('stretch_list', $this->Param);
            ErrorMsg::ApiMsg('请求成功', 0, $json_data);
            break;
        case 'save':
            $data['sr_oid'] = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('订单ID值错误') : $this->Param['oid'];
            $data['sr_cause'] = empty($this->Param['sr_cause']) ? ErrorMsg::ApiMsg('请选择通融原因') : $this->Param['sr_cause'];
            $data['sr_remark'] = $this->Param['sr_remark'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($data['sr_oid'], $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
            $sign_judge = $StretchHelper->signJudge($this->Param['oid']);
            if(!$sign_judge)    ErrorMsg::ApiMsg ('当前状态已不允许再次申请通融');
            
            $data['sr_uid'] = $this->Param['uid'];
            $member = $MemberHelper->getMember($this->Param['uid']);
            $data['sr_uname'] = $member['u_realname'];
            $data['sr_time'] = NOW_TIME;
            $result = $StretchHelper->signSave($data);
            if($result['success'] == true){
                $ApiHelper->logSave('stretch_save', $data);
                ErrorMsg::ApiMsg('保存成功', 0);
            }else{                
                ErrorMsg::ApiMsg($result['msg']);
            }
            break;
        default :
    }
}